package com.gzucm.Scholarship.dto;

import com.alibaba.fastjson.JSON;
import lombok.Getter;
import org.springframework.http.HttpStatus;

import java.io.Serializable;
import java.util.Objects;

@Getter
public class ResultDTO<T> implements Serializable {

    private Boolean success;

    private Integer code;

    private String message;

    private T data;

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public void setCode(HttpStatus status) {
        Objects.requireNonNull(status, "HttpStatus 不能为null");
        this.code = status.value();
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public void setData(T data) {
        this.data = data;
    }

    public static ResultDTO success() {
        return success((String) null);
    }

    public static ResultDTO success(String message) {
        return success(HttpStatus.OK, message, null);
    }

    public static ResultDTO success(Object data) {
        return success(HttpStatus.OK, null, data);
    }

    public static ResultDTO success(String message, Object data) {
        return success(HttpStatus.OK, message, data);
    }

    public static ResultDTO success(HttpStatus status) {
        return success(status, null);
    }

    public static ResultDTO success(HttpStatus status, String message) {
        return success(status, message, null);
    }

    public static ResultDTO success(HttpStatus status, String message, Object data) {
        return new ResultDTO(true, status.value(), message, data);
    }

    public static ResultDTO failure(HttpStatus status) {
        return failure(status, null);
    }

    public static ResultDTO failure(HttpStatus status, String message) {
        return failure(status, message, null);
    }

    public static ResultDTO failure(HttpStatus status, String message, Object data) {
        return new ResultDTO(false, status.value(), message, data);
    }

    private ResultDTO(Boolean success, Integer code, String message, T data) {
        this.success = success;
        this.code = code;
        this.message = message;
        this.data = data;
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }
}
